/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.edu.unicen.twitter.suggest.backend.datasources.dao.util;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 
public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport
{    
    @Autowired
    public void initMethod(SessionFactory sessionFactory)
    {
        setSessionFactory(sessionFactory);
    }
//    public List findbyHQLQuery(final Query hqlQuery){
//        getHibernateTemplate().setAlwaysUseNewSession(true);
//        return getHibernateTemplate().executeFind(new HibernateCallback() {
//            public Object doInHibernate(Session session)
//                    throws HibernateException, SQLException {
//                List list = hqlQuery.list();
//                logger.debug("Find " + list.size() + " records.");
//                return list;
//            }
//        });
//    }
    public List findbyHQL(final String hql){
        getHibernateTemplate().setAlwaysUseNewSession(true);
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                // String str = "from " + persistentClass.getName() + " o";
                Query query = session.createQuery(hql);
                List list = query.list();
                logger.debug("Find " + list.size() + " records.");
                return list;
            }
        });
    }
    public List findbyHQL(final String hql, final List list){
        getHibernateTemplate().setAlwaysUseNewSession(true);
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                // String str = "from " + persistentClass.getName() + " o";
                Query query = session.createQuery(hql).setParameterList("list", list);
                List list = query.list();
                logger.debug("Find " + list.size() + " records.");
                return list;
            }
        });
    }
}